package project.temp_no_use;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import project.function.Function_Webcam;

import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;

public class CreateKey {
    //设置APPID/AK/SK
    public static final String APP_ID = "d7d9b4d037ad4677aedcf318e394bbf8";
    public static final String API_KEY = "zt6d8WTw3i4M7PADay9srXMN";
    public static final String SECRET_KEY = "yElBbG6XwO2cCwyIDytFifZRiknv8pCq";

    public static void main(String[] args) {
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 调用接口
        //String image = "https://baidu-ai.bj.bcebos.com/face/faces.jpg";
        //String imageType = "URL";

        //options是Map，得自己创建
        // 传入可选参数调用接口
        // HashMap<String, String> options = new HashMap<String, String>();
        // options.put("face_field", "age");
        // options.put("max_face_num", "2");
        // options.put("face_type", "LIVE");
        // options.put("liveness_control", "LOW");

        // 人脸检测
        // JSONObject res = client.detect(image, imageType ,options);
        // System.out.println(res.toString(2));
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("max_face_num", "3");
        options.put("match_threshold", "70");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        //options.put("user_id", "842462376");
        options.put("max_user_num", "3");
        JSONObject res = client.search(imageToBase64Str("F:\\IntelliJ IDEA 2021.3.2\\WorkSpace\\Project\\java-sdk-my-project\\src\\main\\resourses\\facePictures\\userFace.png"),"BASE64",Function_Webcam.groupId,options);
        System.out.println(res);
        System.out.println(res.get("result"));
    }

    public static String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        //BASE64Encoder encoder = new BASE64Encoder();
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(data);
    }
}
